<?php

namespace App\Http\Controllers\Admin\Blog;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Blog\Photo;
use App\Models\Blog\Gallery;

class PhotoController extends Controller
{
    function __construct()
    {
        view()->share([
            '_blog' => 'active open',
            '_gallery' => 'active',
            'galleries' => Gallery::orderBy('created_at', 'desc')->get()
        ]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.blog.photo.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:photos|max:45',
            'description' => 'required',
            'image' => 'required',
        ]);

        Photo::create($request->all());

        return redirect(route('blog.photo.show', $request->gallery_id))->with('success', '新增照片成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $galleries = Gallery::with('photos')->find($id);
        return view('admin.blog.photo.show', compact('galleries'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $photo = Photo::find($id);
        return view('admin.blog.photo.edit', compact('photo'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:45',
            'description' => 'required',
            'image' => 'required',
        ]);
        $photo = Photo::find($id);
        $photo->update($request->all());
        return redirect(route('blog.photo.show', $photo->gallery_id))->with('success', '编辑照片成功');
    }

    /**
     * ajax删除照片
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Photo::destroy($id);
    }

    /**
     * ajax改变属性
     * @param Request $request
     */
    public function change_attr(Request $request)
    {
        $like = Photo::find($request->id);
        $attr = $request->attr;
        $like->$attr = !$like->$attr;
        $like->save();
    }

    /***
     * 照片回收站
     */
    public function trash()
    {
        view()->share([
            '_blog' => 'active open',
            '_gallery' => '',
            '_photo_trash' => 'active'
        ]);

        $photos = Photo::with('gallery')->onlyTrashed()->get();
//        return $photos;
        return view('admin.blog.photo.trash', compact('photos'));
    }


    /***
     * ajax永久删除单条
     * @param $id
     */
    public function force_destroy($id)
    {
        Photo::withTrashed()->where('id', $id)->forceDelete();
    }

    /**
     * ajax还原单张照片
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function restore($id)
    {
        Photo::withTrashed()->where('id', $id)->restore();
    }


}
